<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRegistry()
    {
        $config = new Zend_Config_Ini( APPLICATION_PATH . '/configs/application.ini' , 'production', array("allowModifications"=>true) );
        if (file_exists(APPLICATION_PATH . '/configs/local.ini')) {
            $lconfig = new Zend_Config_Ini( APPLICATION_PATH . '/configs/local.ini' , 'production' );
            $config->merge($lconfig);
            $config->setReadOnly();
        }
        
        // build a caching object
        $backend = new Zend_Cache_Backend_Memcached(
                        array('servers' => array( array('host' => '127.0.0.1', 'port' => '11211') ),
                                'compression' => true) );
         
        $frontend = new Zend_Cache_Core(array('caching' => true, 'lifetime' => 3600,
                        'cache_id_prefix' => 'escoreboar', 'automatic_serialization' => true) );
        $cache = Zend_Cache::factory( $frontend, $backend );

        // databases connections
        $main = new Mongo($config->db->main, array("connect"=>false));

        //registry set
        Zend_Registry::set('db_main', $main);

        require_once APPLICATION_PATH . '/models/Sports.php';
        $smodel = new Model_Sports();
        $sportsc = $smodel->find();
        $sports = array();
        foreach ($sportsc as $sport)
        {
            $sports[$sport["_id"]] = $sport;
        }
        Zend_Registry::set('sports', $sports);
    }

    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->setEncoding('UTF-8');
        ZendX_JQuery::enableView($view);
        $view->jQuery()->enable();
        $view->doctype('XHTML1_TRANSITIONAL');
        
        date_default_timezone_set('Europe/Madrid');
    }

    protected function _initAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
                        'namespace' => '',
                        'basePath' => APPLICATION_PATH));

        return $moduleLoader;
    }

    protected function _initPlugins()
    {
        Zend_Controller_Action_HelperBroker::addPath( APPLICATION_PATH .'/controllers/helpers');

        $front = Zend_Controller_Front::getInstance();

        //init the routes
        $router = $front->getRouter ();

        //set the default route url (the default also)
        $route = new Zend_Controller_Router_Route ( ':controller/:action/*', array ('controller' => 'index', 'action' => 'index', 'module' =>'default' ) );
        $events = new Zend_Controller_Router_Route ( 'event/:action/:eventid/', array ('controller' => 'event', 'action' => 'index', 'module' =>'default' ) );
        $routeApi = new Zend_Controller_Router_Route('/api/:action/*', array(  'controller' => 'api', 'action' => 'index') );
        $users = new Zend_Controller_Router_Route ( 'user/:action/*', array ('controller' => 'user', 'action' => 'index', 'module' =>'default' ) );

        $router->addRoute ( 'default', $route ); //important, put the default route first!
        $router->addRoute ( 'events', $events );
        $router->addRoute ( 'users', $users );
        $router->addRoute ( 'api', $routeApi );
        
        //set all routes
        $front->setRouter ( $router );

        return $front;
    }
}
